class Solution(object):
    def myAtoi(self, s):
        """
        :type str: str
        :rtype: int
        """
        sign = None
        valid = True
        INT_MAX = 2147483647
        INT_MIN = -2147483648

        nums = []

        s = s.strip()

        for c in s:
            if c=='-':
                if sign==None: 
                    sign=-1
                else: 
                    valid=False
                    break
            elif c=='+':
                if sign==None:
                    sign=1
                else:
                    valid=False
                    break
            elif ord('0')<=ord(c)<=ord('9'):
                nums.append(int(c))
            else: 
                if len(nums)==0:
                    valid=False
                break
        
        if not valid: return 0
        if not sign: sign = 1

        nums.reverse()
        base = 1
        ret = 0
        for n in nums:
            ret += n * base
            if ret>INT_MAX: break
            base *= 10
        ret *= sign

        ret = min(ret, INT_MAX)
        ret = max(ret, INT_MIN)

        return ret

print(Solution().myAtoi("        -121212esrasfd"))